//***********************************************************************************

// A realistic and relative model, which takes into account all the global 
// unit parameters such as the no of army, no of rebel, no of merchant, no of spy, no of neutral citizens, 
// no of citizens  for war and no of citizens against war to calculate the probability of global event parameters. 
// The outcome of these global event parameters are again reflected back in the global  unit parameters.

// Author: Aravindh U095232L

//***********************************************************************************


function global_distribution ()
{

	//Transactions
	merchants_available = Math.round(Math.random()*3 + (merchant-3));
	console.log("TEST ",army,rebel,for_war,against_war, (army/rebel) , (for_war/against_war) );
	ratio_army_temp =  Math.round((army/rebel) + (for_war/against_war));
	ratio_rebel_temp = Math.round((rebel/army) + (against_war/for_war));
	army_found = Math.round((merchants_available*ratio_army_temp)/(ratio_rebel_temp + ratio_army_temp));
	transactions = merchants_available - army_found;
	weapons = transactions + Math.round(Math.random()*(2));
    rebel_weapons+=weapons;
    army_weapons+=Math.round(weapons/3);

    console.log(merchants_available,ratio_army_temp,ratio_rebel_temp,army_found,transactions,weapons);

	//Fights
	fight_percentage = 0.1;
	max = Math.round(fight_percentage*Math.min(army,rebel));
	temp = Math.round( Math.random()*(3) + (max-3) );
	rebel_winning_temp = Math.round(((rebel/army) + (against_war/for_war) + rebel_weapons));
	army_winning_temp = Math.round(((army/rebel) + (for_war/against_war) + army_weapons));
	army_winning = Math.round((temp*army_winning_temp)/(rebel_winning_temp + army_winning_temp));
	rebel_winning = temp - army_winning;
	army_fight = (army_winning) + (rebel_winning);
	rebel_fight = (rebel_winning) + (army_winning);
	army_weapons-=Math.round(rebel_winning/3);
	rebel_weapons-=Math.round(army_winning/3);

	if(army_weapons<0)
		army_weapons = 0;
	else if(rebel_weapons<0)
		rebel_weapons = 0;

	army-=Math.round(rebel_fight);
	rebel-=Math.round(army_fight);

	console.log(max, temp,rebel_winning_temp,army_winning_temp, rebel_winning, army_winning, army_fight, rebel_fight, army_weapons);
		
	//TV station
	tv_station_idle = 0.25;
	rebel_control_temp = Math.round((rebel/army) + (against_war/for_war) + rebel_weapons);
	army_control_temp = Math.round((army/rebel) + (for_war/against_war) + army_weapons + spy/4);
	tv_station_working = 1 - tv_station_idle;
	army_control = (tv_station_working*army_control_temp)/(rebel_control_temp + army_control_temp);
	rebel_control = tv_station_working - army_control;
	random_control = Math.random();
	if(random_control< army_control)
	{
		if(against_war>citizen)
			for_war = citizen - 30;
		else if((citizen - for_war) < 110)
			for_war += Math.round(Math.random()*10) ;
		else
			for_war += Math.round( Math.random()*35 +(10) );

		if( (against_war+for_war) >= citizen)
			against_war = citizen - for_war;
		else if (against_war > citizen)
			against_war = citizen - 30;
		else if ((for_war) < 50)
			against_war -= Math.round(Math.random()*5) ;
		else
			against_war -= Math.round(Math.random()*20);

		if(against_war <= 0)
			against_war = 5;
	}
	else if ( (random_control>army_control) && (random_control<0.75) )	
	{
		if(against_war>citizen)
			against_war = citizen - 20;
		else if((citizen - against_war) < 110)
			against_war += Math.round(Math.random()*10) ;
		else
			against_war += Math.round( Math.random()*50 +(10) );

		if( (against_war+for_war) >= citizen)
			for_war = citizen - against_war;
		else if (for_war > citizen)
			for_war = citizen - 30;
		else if ((for_war) < 50)
			for_war -= Math.round(Math.random()*5) ;
		else
			for_war -= Math.round(Math.random()*20);

		if(for_war <=0)
			for_war = 5;
	}

	console.log(rebel_control_temp,army_control_temp,army_control, rebel_control, random_control, for_war, against_war);


	//Personal
	people_influenced = citizen;
	rebel_personal_temp = Math.round((rebel/army) + (against_war/for_war) + rebel_weapons);
	army_personal_temp = Math.round((for_war/against_war) + army_weapons + spy);
	spy_influence_temp = (people_influenced*army_personal_temp)/(rebel_personal_temp + army_personal_temp);
	rebel_influence_temp = people_influenced - spy_influence_temp;
	ratio_rebel = rebel_personal_temp/people_influenced;
	ratio_spy = spy_influence_temp/people_influenced;	
	rebel_influence = Math.round(Math.random()*50 + (rebel_personal_temp/100));
	spy_influence = Math.round(Math.random()*50 + (spy_influence_temp/100));
	against_war+=rebel_influence;
	for_war+=spy_influence;
	against_war-=Math.round(spy_influence*5/rebel_influence);
	for_war-=Math.round(rebel_influence*5/spy_influence);
	if(against_war>(citizen-for_war))
		against_war = citizen - for_war;
	else if (for_war>(citizen- against_war))
		for_war = citizen - against_war;


	if(against_war <= 0)
		against_war = 5;
	if(for_war <=0)
		for_war = 5;

	console.log(rebel_weapons, army_weapons, rebel_influence_temp, spy_influence_temp ,rebel_influence, spy_influence,for_war,against_war);

	//Roadshows
	rebel_show_temp = Math.round((rebel/army) + (against_war/for_war) + rebel_weapons);
	army_show_temp = Math.round(((army/rebel) + (for_war/against_war) + army_weapons));
	show_ratio = rebel_show_temp/army_show_temp;
	if(show_ratio>=1)
		against_war+=Math.round(Math.random()*30 + 15);
	else
		against_war+=Math.round(Math.random()*5 + 3);

	if(against_war>(citizen-for_war))
		against_war = citizen - for_war;
	else if (for_war>(citizen- against_war))
		for_war = citizen - against_war;

	if(for_war >1000 )
		for_war = 900;
	else if (against_war > 1000)
		against_war = 900;
	if(against_war <= 0)
			against_war = 5;
	if(for_war <=0)
		for_war = 5;

	console.log(for_war, against_war);
	
	//Confusion
	if(Math.abs(for_war-against_war)<100)
	if (Math.random()<0.4)
	{	for_war = Math.round(Math.random()*75+10);
		against_war = Math.round(Math.random()*75+10);
	}


	if( (for_war+against_war)>citizen)
	{
		if(for_war>=against_war)
			for_war = (citizen - against_war - 20);
		else
			against_war = (citizen - for_war - 20);
	}

}